﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ProtoBuf;

namespace ZyGames.DZPocker.Model
{
	/// <summary>
	/// 牌组
	/// </summary>
	[Serializable, ProtoContract]
	public class CardGroup
	{
		private const int MaxLength = 20;
		/// <summary>
		/// 包括None类型一共五种花色
		/// </summary>
		private const int MaxColorLength = 5;
		/// <summary>
		/// 几张数量才是炸弹
		/// </summary>
		private const int BombMinNum = 4;
		/// <summary>
		/// 每一种花色最大的牌的数量
		/// </summary>
		private const int ColorMinNum = 13;
		/// <summary>
		/// 大小牌组
		/// </summary>
		private List<int>[] _list;
		/// <summary>
		/// 花色牌组
		/// </summary>
		private List<int>[] _colorList;

		/// <summary>
		/// 
		/// </summary>
		/// <param name="packNum">几副牌</param>
		public CardGroup()
		{
			_list = new List<int>[MaxLength];
			_colorList = new List<int>[MaxColorLength];
			for (int i = 0; i < MaxLength; i++)
			{
				_list[i] = new List<int>(BombMinNum);
			}
			for (int j = 0; j < MaxColorLength; j++)
			{
				_colorList[j] = new List<int>(ColorMinNum);
			}
		}

		public int Count
		{
			get { return _list.Length; }
		}

		/// <summary>
		/// 获得牌型的花色种类
		/// </summary>
		public int CardColorCount
		{
			get { return _colorList.Length; }
		}

		/// <summary>
		/// 获得同一大小牌的数量
		/// </summary>
		/// <param name="cardSize"></param>
		/// <returns></returns>
		public int[] GetSame(int cardSize)
		{
			return GetSame((CardSize)cardSize);
		}

		/// <summary>
		/// 根据数量查找大于等于该数量的牌组
		/// </summary>
		/// <param name="cardNum"></param>
		/// <returns>bool</returns>
		public int[] GetSameColorGroupByMinNum(int count)
		{
			foreach (var item in _colorList)
			{
				if (item.Count >= count)
				{
					return item.ToArray();
				}
			}
			return new int[0];
		}

		/// <summary>
		/// 获得同一大小的牌组
		/// </summary>
		/// <param name="cardSize"></param>
		/// <returns></returns>
		public int[] GetSame(CardSize cardSize)
		{
			return _list[(int)cardSize].ToArray();
		}

		/// <summary>
		/// 获得同一种花色的牌组
		/// </summary>
		/// <param name="cardColor"></param>
		/// <returns></returns>
		public int[] GetSameColor(CardColor cardColor)
		{
			return _colorList[(int)cardColor].ToArray();
		}

		/// <summary>
		/// 获得相同值个数的牌组
		/// </summary>
		/// <param name="count">相同的个数</param>
		/// <returns></returns>
		public int[] FindSame(int count)
		{
			foreach (var item in _list)
			{
				if (item.Count == count)
				{
					return item.ToArray();
				}
			}
			return new int[0];
		}

		/// <summary>
		/// 找到对应花色的牌组
		/// </summary>
		/// <param name="cardColor"></param>
		/// <returns></returns>
		public int[] FindSameColor(CardColor cardColor)
		{
			return _colorList[(int)cardColor].ToArray();
		}

		/// <summary>
		/// 查找相同个数
		/// </summary>
		/// <param name="count"></param>
		/// <returns></returns>
		public int[] FindSameCount(int count)
		{
			List<int> sizeList = new List<int>();
			for (int i = 0; i < _list.Length; i++)
			{
				var item = _list[i];
				if (item.Count == count)
				{
					sizeList.Add(i);
				}
			}
			return sizeList.ToArray();
		}

		/// <summary>
		/// 查找相同个数
		/// </summary>
		/// <param name="count"></param>
		/// <returns></returns>
		public List<int[]> FindSameCountAndGroup(int count)
		{
			List<int[]> sizeList = new List<int[]>();
			for (int i = 0; i < _list.Length; i++)
			{
				var item = _list[i];
				if (item.Count == count)
				{
					sizeList.Add(item.ToArray());
				}
			}
			return sizeList;
		}

		/// <summary>
		/// 添加一张牌
		/// </summary>
		/// <param name="card">牌Id</param>
		/// <param name="cardSize">牌大小</param>
		public void Add(int card, CardSize cardSize, CardColor cardColor = CardColor.None)
		{
			_list[(int)cardSize].Add(card);
			_colorList[(int)cardColor].Add(card);
		}

		public int GetSameSizeCount(int cardSize)
		{
			return GetSame(cardSize).Length;
		}

		/// <summary>
		/// 根据牌值大小来获得数量
		/// </summary>
		/// <param name="cardSize"></param>
		/// <returns></returns>
		public int GetSameSizeCount(CardSize cardSize)
		{
			return GetSame(cardSize).Length;
		}
	}
}
